package com.googlecode.mgwt.expenses.client;

import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.History;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.event.shared.HandlerRegistration;
import com.googlecode.mgwt.expenses.client.event.AppEvent;
import com.googlecode.mgwt.expenses.client.event.AppEvent.AppEvents;
import com.googlecode.mgwt.expenses.client.event.ExpenseEntryCreatedEvent;
import com.googlecode.mgwt.expenses.client.event.ExpenseEntrySelectedEvent;
import com.googlecode.mgwt.expenses.client.places.ExpensePlace;
import com.googlecode.mgwt.mvp.client.history.HistoryHandler;
import com.googlecode.mgwt.mvp.client.history.HistoryObserver;
import com.googlecode.mgwt.ui.client.MGWT;
import com.googlecode.mgwt.ui.client.event.ShowMasterEvent;

public class AppHistoryObserver implements HistoryObserver, ContextProvider {

	private final PlaceController placeController;

	@Inject
	public AppHistoryObserver(PlaceController placeController) {
		this.placeController = placeController;
	}

	@Override
	public void onPlaceChange(Place place, HistoryHandler handler) {

	}

	@Override
	public void onHistoryChanged(Place place, HistoryHandler handler) {

	}

	@Override
	public void onAppStarted(Place place, HistoryHandler historyHandler) {
		if (MGWT.getOsDetection().isDesktop()) {
			onDesktopNav(place, historyHandler);
		} else {
			if (MGWT.getOsDetection().isPhone()) {
				onPhoneNav(place, historyHandler);
			} else {
				// tablet
				onTabletNav(place, historyHandler);

			}
		}

	}

	private void onDesktopNav(Place place, HistoryHandler historyHandler) {

	}

	@Override
	public HandlerRegistration bind(final EventBus eventBus, final HistoryHandler historyHandler) {

		AppEvent.register(eventBus, AppEvents.BACK, new AppEvent.Handler() {

			@Override
			public void onEvent(AppEvent event) {

			}
		});

		AppEvent.register(eventBus, AppEvents.EXPENSES, new AppEvent.Handler() {

			@Override
			public void onEvent(AppEvent event) {

				if (MGWT.getOsDetection().isPhone()) {
					History.back();
				} else {
					eventBus.fireEvent(new ShowMasterEvent("nav"));
				}

			}
		});

		ExpenseEntrySelectedEvent.register(eventBus, new ExpenseEntrySelectedEvent.Handler() {

			@Override
			public void onExpenseEntrySelected(ExpenseEntrySelectedEvent event) {

				historyHandler.goTo(new ExpensePlace(event.getId(), true, false));

			}
		});

		ExpenseEntryCreatedEvent.register(eventBus, new ExpenseEntryCreatedEvent.Handler() {

			@Override
			public void onExpenseEntrySelected(ExpenseEntryCreatedEvent event) {

				historyHandler.goTo(new ExpensePlace(event.getId(), true, true));

			}
		});

		return null;
	}

	private void onPhoneNav(Place place, HistoryHandler historyHandler) {

		if (place instanceof ExpensePlace) {
			ExpensePlace expensePlace = (ExpensePlace) place;

			if (expensePlace.isDetail()) {
				historyHandler.replaceCurrentPlace(ExpensePlace.DEFAULT_PLACE);
			}

		}

	}

	private void onTabletNav(Place place, HistoryHandler historyHandler) {

	}

	@Override
	public String getCurrentExpsenseId() {
		Place where = placeController.getWhere();
		if (where instanceof ExpensePlace) {
			ExpensePlace expensePlace = (ExpensePlace) where;
			return expensePlace.getId();
		}
		throw new RuntimeException("we should never get here!");
	}

	@Override
	public Mode getMode() {
		Place where = placeController.getWhere();
		if (where instanceof ExpensePlace) {
			ExpensePlace expensePlace = (ExpensePlace) where;
			return expensePlace.isEdit() ? Mode.EDIT : Mode.DISPLAY;
		}
		throw new RuntimeException("we should never get here!");
	}

}
